<template>
  <div>
    <el-container>
      <el-header>
        <el-breadcrumb separator=">">
          <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>商品管理</el-breadcrumb-item>
          <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>
      </el-header>
      <el-main>
        <div class="search">
          <el-input
            class="input"
            v-model="input"
            placeholder="请输入内容"
          ></el-input>
          <el-button
            @click="search"
            type="primary"
            icon="el-icon-search"
          ></el-button>
          <el-button @click="add" type="primary">添加商品</el-button>
        </div>
        <!-- 渲染 -->
        <el-table :data="list" border style="width: 100%">
          <el-table-column align="center" label="" width="50">
            <template slot-scope="{ $index }">
              {{ $index + 1 }}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="goods_name"
            label="商品名称"
            width="500"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="goods_price"
            label="商品价格(元)"
          ></el-table-column>
          <el-table-column align="center" prop="goods_weight" label="商品重量">
          </el-table-column>
          <el-table-column align="center" prop="add_time" label="创建时间">
            <template slot-scope="{ row }">
              {{ new Date(row.add_time).toLocaleString() }}
            </template>
          </el-table-column>

          <el-table-column align="center" label="操作" width="300">
            <template slot-scope="{ row }">
              <!-- {{ row }} -->
              <div class="actions">
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  @click="bj(row)"
                ></el-button>
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  @click="del(row.goods_id)"
                ></el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="100"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
        <!-- 编辑对话框 -->
        <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
          <el-form
            :model="ruleForm"
            status-icon
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="商品名称" prop="goods_name">
              <el-input
                type="text"
                v-model="ruleForm.goods_name"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <el-form-item label="价格" prop="goods_price">
              <el-input
                type="text"
                v-model="ruleForm.goods_price"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <el-form-item label="数量" prop="goods_number">
              <el-input
                type="text"
                v-model="ruleForm.goods_number"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <el-form-item label="重量" prop="goods_weight">
              <el-input
                type="text"
                v-model="ruleForm.goods_weight"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <el-form-item label="商品介绍" prop="goods_introduce">
              <el-input
                type="text"
                v-model="ruleForm.goods_introduce"
                autocomplete="off"
              ></el-input>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')"
                >提交</el-button
              >
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </el-dialog>
      </el-main>
    </el-container>
  </div>
</template>
<script>
import { getGoodsApi, delGoodsApi, chaApi, bjgoodsApi } from '@/api/goods'
export default {
  name: 'goods',
  data () {
    return {
      input: '',
      list: [],
      total: 0,
      obj: {
        pagesize: 5,
        pagenum: 1,
        query: ''
      },
      // 对话框
      dialogVisible: false,
      ruleForm: {
        goods_id: '',
        goods_name: '',
        goods_price: '',
        goods_number: '',
        goods_weight: '',
        goods_introduce: ''
      },
      rules: {
        goods_name: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
        ],
        goods_price: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
        ],
        goods_number: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
        ],
        goods_weight: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
        ],
        goods_introduce: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
        ],
      }
    }
  },
  created () {
    this.getGoodsList()
  },
  computed: {
  },
  methods: {
    async getGoodsList () {
      const res = await getGoodsApi(this.obj)
      // console.log(res, 78787878)
      this.list = res.data.goods
      this.total = res.data.total
      this.$message({
        message: '获取商品列表成功！',
        type: 'success'
      });
    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`);
      this.obj.pagesize = val
      this.getGoodsList()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`);
      this.obj.pagenum = val
      this.getGoodsList()
    },
    search () {
      this.obj.query = this.input
      this.getGoodsList()
    },
    async del (id) {

      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await delGoodsApi(id)
        // console.log(res, 8989898989)
        this.getGoodsList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    add () {

      this.$router.push('/goods/add')
    },
    // 编辑
    async bj (row) {
      this.dialogVisible = true
      const res = await chaApi(row.goods_id)
      this.ruleForm = res.data
    },
    submitForm (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          await bjgoodsApi(this.ruleForm)
          this.getGoodsList()
          this.dialogVisible = false
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm (formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>
<style lang='scss'  scoped>
.search {
  display: flex;
  .input {
    width: 200px;
  }
}
</style>
